import angular from 'angular';
import swal from 'sweetalert2';

export default angular.module('app.controllers', [])

    .controller('homeCtrl', function ($rootScope, $scope, $state) {


    })
    //注册一个控制器

    .controller('signupCtrl', function ($scope, UserService, $location) {
        $scope.content = {
            "name": "",
            "pwd": ""
        };
        $scope.doSignup = function () {
            if ($scope.content.name == "" || $scope.content.pwd == "") {
                alert("用户名名或密码为空，请重新输入！");
            }
            else {
                let promise = UserService.register($scope.content);
                promise.then(function (data) {
                    console.log(data);
                    // if (data.code==0) {
                    //     $location.path("/home");
                    // }
                    console.log($scope.doSignup);
                });
            }
        }
    })
    .controller('loginCtrl', function ($scope, $http, UserService, $state, $location, SystemService) {
        //$scope.content = {};
        $scope.doLogin = function () {
            let promise = UserService.login($scope.content);
            promise.then(function (data) {
                console.log(data.data);
                console.log(data.data.returnValue._id);
                SystemService.setUser(data.data.returnValue);
                SystemService.setUID(data.data.returnValue._id);
                console.log(data);
                if (data.data.code == 0) {
                    $state.go('showtime');
                }
            });
        }
    })
    .controller('aboutmeCtrl', function ($scope, UserService, $location) {
        $scope.doLogin = function () {
            if ($scope.signupForm.$valid) {
                userServices.signup($scope.signup).then(function (result) {
                    $scope.data = result;
                    if (!result.error) {
                        $location.path("/login");
                    }
                });
            }
        }
    })
    .controller('showtimeCtrl', function ($scope, $state, UserService, $location, SystemService) {


        $scope.week = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
        $scope.timetable = {
            Mon: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Tue: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Wed: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Thur: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Fri: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Sat: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
            Sun: {
                MorningStart: "00:00",
                MorningEnd: "00:00",
                AfternoonStart: "00:00",
                AfternoonEnd: "00:00",
                EveningStart: "00:00",
                EveningEnd: "00:00",
            },
        }

        var userId = SystemService.getUID();

        var param = {
            userId: userId,
            timetable: $scope.timetable
        };

        console.log($scope.timetable);
        var promise = UserService.getTimetable(param);

        promise.then(function (data) {

            if (data && data.timetable) {
                $scope.timetable = data.timetable;
            }

        }, function (err) {
            swal("抱歉,无法完成操作", err, "warning");
        });

        $scope.editTimetable = function (d, timePoint) {

            swal({
                title: '提示',
                text: '请输入时间',
                type: 'info',
                input: 'text',
                inputPlaceholder: "hh:ii",
                showCancelButton: true,
                confirmButtonText: 'confirm',
                cancelButtonText: 'cancel'
            }).then(function (inputValue) {
                if (/^(?:[01]?\d|2[0-4]):(?:[0-5]?\d|60)$/.test(inputValue) || inputValue == "") {

                    $scope.timetable[d][timePoint] = inputValue;
                    $scope.$apply();

                    /* var userId=SystemService.getUID();
                     var timetable=angular.toJson($scope.timetable,true );
                     var param = {
                     user:userId,
                     timetable:timetable
                     }

                     console.log(param)
                     console.log(typeof (param))*/

                }
                else {
                    swal('Oops...', '格式错误！', 'error');
                }
                //console.log(inputValue;
            }, function (dismiss) {
                if (dismiss === 'cancel') {
                    // ignore
                } else {
                    console.log(dismiss);
                }
            });
        }

        $scope.saveTimetable = function () {

            param = {
                userId: userId,
                timetable: $scope.timetable
            };
            let promise = UserService.saveTimetable(param);
            promise.then(function (data) {
                console.log("success!");
                swal({title: "保存成功！", type: "success"})
            }, function (data) {
                swal("抱歉,无法完成操作", data, "warning");
            })

        }


    })


    .name;